package me.zhengjie.modules.weixin.callback;

import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.CourseOrder;
import me.zhengjie.modules.system.domain.IdentityItem;
import me.zhengjie.modules.system.domain.IdentityOrder;
import me.zhengjie.modules.system.service.IdentityOrderService;
import me.zhengjie.modules.system.service.dto.WxUserDto;
import me.zhengjie.modules.system.service.mapstruct.WxUserExpMapper;
import me.zhengjie.modules.web.domain.dis.DisOrderCalcDTO;
import me.zhengjie.modules.web.service.dis.IDisOrderWebService;
import me.zhengjie.modules.web.service.mybatisplus.IdentityItemPlusService;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Objects;

import static me.zhengjie.constant.ConstantUtils.DisOrderType_Identity;

@Service
public class IdentityCallbackServiceImpl implements IPayCallbackService{
    private static final Logger log = LoggerFactory.getLogger(IdentityCallbackServiceImpl.class);
    @Autowired
    private IdentityOrderService orderService;
    @Autowired
    private WxUserExpMapper wxUserExpMapper;
    @Autowired
    private IDisOrderWebService disOrderWebService;

    @Autowired
    private IdentityItemPlusService identityItemPlusService;
    @Override
    public OrderPrefixEnum prefix() {
        return OrderPrefixEnum.Identity;
    }

    @Override
    public boolean execute(WxPayOrderNotifyResult notifyResult) {
        IdentityOrder one = orderService.findOne(notifyResult.getOutTradeNo());
        if (one.getIdentityAmount().multiply(BigDecimal.valueOf(100)).compareTo(BigDecimal.valueOf(notifyResult.getTotalFee())) != 0){
            log.error("支付金额和订单金额不一致");
            return false;
        }
        one.setPayAmount(new BigDecimal(notifyResult.getTotalFee()).divide(new BigDecimal(100)));
        one.setPaySerialid(notifyResult.getTransactionId());
        one.setPayStatus(ConstantUtils.Order_Payed);
        one.setOrderStatus(ConstantUtils.Order_Payed);
        one.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        orderService.update(one);

        LambdaQueryWrapper<IdentityItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdentityItem::getItemNo,one.getBizId());
        IdentityItem identityItem = identityItemPlusService.getOne(wrapper);
        WxUserDto wxUserDto = new WxUserDto();
        wxUserDto.setIdentityNo(one.getBizId());
        wxUserDto.setUpdateTime(DateUtil.TimestampNow());
        String title = "";
        if (Objects.nonNull(identityItem)){
            title = identityItem.getTitle();
            wxUserDto.setIdentityName(identityItem.getTitle());
            wxUserDto.setTicketNumber(identityItem.getTicketNum());
        }else {
            log.warn("根据%s未查询到身份产品信息",one.getBizId());
        }
        LambdaQueryWrapper<WxUserDto> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(WxUserDto::getOpenId,one.getOpenId());
        wxUserExpMapper.update(wxUserDto,userWrapper);
        String finalTitle = title;
        ThreadUtil.execAsync(() -> {
            //计算佣金
            DisOrderCalcDTO disOrderCalcDTO = new DisOrderCalcDTO();
            disOrderCalcDTO.setOrderSn(one.getOrderNo());
            disOrderCalcDTO.setOrderAmount(one.getIdentityAmount());
            disOrderCalcDTO.setOpenId(one.getOpenId());
            disOrderCalcDTO.setOrderType(DisOrderType_Identity);
            disOrderCalcDTO.setTitle(finalTitle);
            disOrderWebService.insertDisOrder(disOrderCalcDTO);
        });
        return true;
    }
}
